<template>
  <div class="home-index">
    <div class="quit">
      <div class="quit_div" @click="quit" v-throttle:click>退出</div>
    </div>
    <div class="home-index-card">
      <van-image round class="image" :src="userInfo?.avatar" />
      <div class="home-index-card-info">
        <div class="home-index-card-info-name">{{ userInfo?.nickName }}</div>
        <div class="home-index-card-info-position">
          {{ userInfo.dept?.deptName }} | {{ positionName(userPost) }}
        </div>
      </div>
    </div>
    <div class="home-index-content">
      <div class="home-index-content-top">
        <div class="home-index-content-top-title">
          <van-icon name="apps-o" color="#1466FF" size="20" />
          <div class="home-index-content-top-type">快捷方式</div>
        </div>
        <div class="home-index-content-top-content">
          <div v-for="(item, index) in btnList" :index="index" class="content-div">
            <div class="circle" @click="addProduct(item.name)">
              <van-image :src="item.img" />
            </div>
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="home-index-content-center">
        <div class="title">待办工作</div>
        <div class="home-index-content-center-items">
          <div class="items_items" v-for="(item, index) in centerList" :key="index" @click="toProductList(item.id)">
            <van-badge :content="item.nums" :show-zero="false">
              <div class="circle">
                <van-image :src="item.img" />
              </div>
              <div>{{ item.name }}</div>
            </van-badge>
          </div>
        </div>
      </div>
      <div class="home-index-content-bottom">
        <div class="title">数据统计
          <!-- <van-date-picker v-model="currentDate" title="日期选择" /> -->
          <van-cell title="选择日期区间" :value="date" @click="show = true" />
          <van-calendar v-model:show="show" type="range" @confirm="onConfirm" />
        </div>
        <div class="content">
          <div class="datas">
            <div v-for="(item, index) in topData" :key="index">
              <p class="num" :style="{ 'color': item.color }">{{ item.num }}</p>
              <p class="name">{{ item.name }}</p>
            </div>
          </div>
          <div class="echarts">
            <line_echarts1 :styleClass="{ height: '150px' }" :chartData="ring1Data" class="ring_char_box">
            </line_echarts1>
          </div>
          <div class="datas">
            <div v-for="(item, index) in bottomData" :key="index">
              <p class="num" :style="{ 'color': item.color }">{{ item.num }}</p>
              <p class="name">{{ item.name }}</p>
            </div>
          </div>
          <div class="echarts">
            <line_echarts2 :styleClass="{ height: '150px' }" :chartData="ring1Data" class="ring_char_box">
            </line_echarts2>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import line_echarts1 from "./components/line_echarts1.vue";
import line_echarts2 from "./components/line_echarts2.vue";
import { getInfo } from "@/api/supplierList/index";
import { apiLogout } from "@/api/loginApi";
import { loginStore } from "@/store/login";
import { showConfirmDialog } from "vant";
import { getTotal } from "@/api/productList/index";

const login = loginStore();
const router = useRouter();
const show = ref(false);
const date = ref("");
const userInfo = ref<any>({});
const userPost = ref<any>([])
const roles = ref<any>({});
const formatDate = (date) => `${date.getMonth() + 1}/${date.getDate()}`;
const onConfirm = (values) => {
  const [start, end] = values;
  show.value = false;
  date.value = `${formatDate(start)} - ${formatDate(end)}`;
};

const topData = ref([
  {
    name: "增品",
    num: 789,
    color: "#0BC23C",
  },
  {
    name: "上架",
    num: 789,
    color: "#7D07FF",
  },
  {
    name: "建联",
    num: 789,
    color: "#1466FF",
  },
]);
const bottomData = ref([
  {
    name: "选中",
    num: 789,
    color: "#0BC23C",
  },
  {
    name: "入库",
    num: 789,
    color: "#7D07FF",
  },
  {
    name: "签合同",
    num: 789,
    color: "#1466FF",
  },
]);
const ring1Data = reactive([
  {
    value: 10,
    name: "粉丝",
  },
  {
    value: 20,
    name: "非粉丝",
  },
]);

const btnList = ref([
  {
    name: "添加供应商",
    img: new URL("@/assets/home/addSupplier.png", import.meta.url).href,
  },
  {
    name: "添加商品",
    img: new URL("@/assets/home/addCommodity.png", import.meta.url).href,
  },
  {
    name: "邀约样品",
    img: new URL("@/assets/home/addSample.png", import.meta.url).href,
  },
]);
const centerList = ref([
  {
    name: "待收货",
    nums: 0,
    id: "1",
    img: new URL("@/assets/home/warehousing.png", import.meta.url).href,
  },
  {
    name: "待上架",
    nums: 0,
    id: "4",
    img: new URL("@/assets/home/pendingListing.png", import.meta.url).href,
  },
  {
    name: "待送审",
    nums: 0,
    id: "6",
    img: new URL("@/assets/home/pendingReview.png", import.meta.url).href,
  },
  {
    name: "待入库 ",
    nums: 0,
    id: "8",
    img: new URL("@/assets/home/pendingStored.png", import.meta.url).href,
  },
  {
    name: "待议价",
    nums: 0,
    id: "9",
    img: new URL("@/assets/home/pendingBargaining.png", import.meta.url).href,
  },
  // {
  //   name: '已超期 ', nums: 0, img: new URL('@/assets/home/warehousing.png', import.meta.url).href
  // },
]);
const addProduct = (name: string) => {
  if (name == "添加商品") {
    router.push("/addProduct");
  } else if (name == "添加供应商") {
    router.push("/addSupplier");
  } else if (name == "邀约样品") {
    router.push("/sample");
  }
};

const getList = async () => {
  const res = await getInfo();
  roles.value = res.data.user.roles[0];
  userInfo.value = res.data.user;
  userPost.value = res.data.posts || [];
  const total = await getTotal();
  centerList.value[0].nums = total.data[1].total;
  centerList.value[1].nums = total.data[4].total;
  centerList.value[2].nums = total.data[6].total;
  centerList.value[3].nums = total.data[8].total;
  centerList.value[4].nums = total.data[9].total;
};
getList();

/** 跳转 */
const toProductList = (id: string) => {
  router.push({ path: "/productList", query: { id: id } });
};

/** 退出 */
const quit = () => {
  showConfirmDialog({
    message: "是否确认退出账号",
  }).then(async () => {
    const res = await apiLogout();
    if (res.code === 200) {
      login.resetToken();
      router.push("/login");
    }
  });
};
// 反显职位权限名称
const positionName = (data: any) => {
  let name: any = []
  data.forEach((item: any) => {
    name.push(item.postName)
  })
  return name.join('/')
}
</script>

<style lang="less" scoped>
.home-index {
  width: 100%;
  height: calc(100vh - 50px);
  background-color: rgba(238, 238, 238);
  overflow: auto;

  .quit {
    font-size: 12px;
    background: rgba(34, 135, 255);
    color: #fff;
    padding: 10px 20px 0 0;
    width: 100%;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-end;

    .quit_div {
      width: 27px;
    }
  }

  &-card {
    height: 25%;
    width: 100%;
    background: rgba(34, 135, 255);
    display: flex;
    align-items: center;
    padding-left: 20px;
    box-sizing: border-box;

    .image {
      width: 60px;
      height: 60px;
      margin-bottom: 30px;
    }

    &-info {
      margin-left: 20px;
      margin-bottom: 30px;

      &-name {
        font-size: 16px;
        color: #fff;
        margin-bottom: 5px;
      }

      &-position {
        font-size: 14px;
        color: rgba(192, 221, 255);
      }
    }
  }

  &-content {
    min-height: calc(90% - 50px);
    margin: 0 20px;
    margin-top: -15%;
    // display: flex;
    // flex-wrap: wrap;
    // align-content: space-between;

    &-top {
      width: 100%;
      height: 18%;
      background: #fff;
      border-radius: 20px;
      padding: 10px;
      box-sizing: border-box;

      &-content {
        font-size: 12px;
        width: 100%;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        height: calc(100% - 30px);

        .content-div {
          display: flex;
          flex-wrap: wrap;
          flex-direction: column;
          align-items: center;
        }

        .circle {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          // background-color: #599FFF;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 5px;

          :deep(.van-image) {
            width: 40px;
            height: 40px;
          }
        }
      }

      &-title {
        display: flex;
        align-items: center;
        height: 30px;
      }

      &-type {
        font-size: 14px;
        margin-left: 5px;
      }
    }

    &-center {
      width: 100%;
      height: 113px;
      box-sizing: border-box;
      border-radius: 20px;
      background: #fff;
      padding: 10px;
      padding-top: 5px;
      margin-top: 20px;

      // display: flex;
      // align-items: center;
      .title {
        font-size: 12px;
        height: 18px;
      }

      &-items {
        width: 100%;
        height: calc(100% - 18px);
        display: flex;
        justify-content: space-between;
        align-items: center;

        .items_items {
          width: 16%;
          height: 100%;
          font-size: 10px;
          display: flex;
          justify-content: center;
          align-items: center;
          flex-wrap: wrap;

          .circle {
            width: 35px;
            height: 35px;
            // border-radius: 50%;
            // background-color: #FFD6D6;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 5px;

            :deep(.van-image) {
              width: 25px;
              height: 25px;
            }
          }
        }
      }
    }

    &-bottom {
      width: 100%;
      margin-top: 20px;
      // height: 65%;
      box-sizing: border-box;
      background: #fff;
      border-top-left-radius: 20px;
      border-top-right-radius: 20px;
      padding: 10px;
      min-height: 65%;

      .content {
        .echarts {
          width: 100%;

          .ring_char_box {
            width: 100%;
          }
        }

        .datas {
          width: 100%;
          height: 35px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          text-align: center;
          padding: 0 10%;
          box-sizing: border-box;

          p {
            margin: 0;
          }

          .num {
            font-size: 20px;
          }

          .name {
            font-size: 10px;
          }
        }
      }

      .title {
        font-size: 12px;
        height: 22px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        :deep(.van-cell) {
          width: 180px;
          font-size: 12px;
          line-height: 12px;
          padding-bottom: 0;
          padding-top: 1px;
        }
      }
    }
  }
}
</style>
